#!/usr/bin/python
# -*- encoding: utf-8 -*-
'''
@File    :   6.py
@Time    :   2020/05/03 19:16:54
@Author  :   Malygos_l 
@Version :   1.0
@Contact :   751756061@qq.com
@WebSite :   www.gitee.com/malygos_l
'''
# 在2个文件中存放了英文计算机技术文章(可以选择2篇关于Python技术文件操作处理技巧的2篇英文技术文章), 请读取文章内容,进行词频的统计;并分别输出统计结果到另外的文件存放;
# 比较这2篇文章的相似度(如果词频最高的前10个词,重复了5个,相似度就是50%;重复了6个,相似度就是60% ,......);
# Start typing your code from here

def getText(f): 
    f = f.read().lower()         
    for ch in "~@#$%^&*()_-+=<>?/,.:;{}[]|\'""":    
        f = f.replace(ch,' ')       
    return f 

def count(f,dic,list):
    words = getText(f).split()
    for word in words:
        dic[word] = dic.get(word,0) + 1
    list.append(sorted(dic.items(),key=lambda x: x[1],reverse = True))

try:
    with open('homework3\\atc1.txt','r') as f:
        count1 = {}
        sortct1 = []
        count(f,count1,sortct1)

    with open('homework3\\atc2.txt','r') as f:
        count2 = {}
        sortct2 = []
        count(f,count2,sortct2)

    similar = 0
    for i in range(10):
        for k in range(10):
            if(sortct1[0][i][0] == sortct2[0][k][0]):
                similar += 1
    print("相似度是",similar*10,"%")

except IOError:
    print("ERROR!")